define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user/user/index',
                    add_url: 'user/user/add',
                    edit_url: 'user/user/edit',
                    // del_url: 'user/user/del',
                    multi_url: 'user/user/multi',
                    import_url: 'user/user/import',
                    table: 'user',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'user.id',
                fixedColumns: true,
                fixedRightNumber: 1,
                searchFormVisible:true,
                search:false,
                columns: [
                    [
                        {checkbox: true},
                        // {field: 'id', title: __('Id'), sortable: true,operate:false},
                        {field: 'username', title: __('学号'), operate: 'LIKE'},
                        {field: 'nickname', title: __('姓名'), operate: 'LIKE'},
                        {field: 'gender', title: __('Gender'),  searchList: {1: __('Male'), 0: __('Female')},
                            formatter:function (value,row,index) {
                                if(value==1){
                                    return '<span class="text-success" >男</span>';
                                }
                                if(value==0){
                                    return '<span class="text-info" >女</span>';
                                }
                            },
                        },
                        {field: 'grade', title: __('年级'), operate: 'LIKE'},
                        {field: 'classed', title: __('班级'), operate: 'LIKE'},

                        {field: 'jointime', title: __('Jointime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {field: 'status', title: __('Status'), formatter: Table.api.formatter.status, searchList: {normal: __('Normal'), hidden: __('Hidden')}},
                        {field: 'test_text', title: __('已完成测评'), operate:false,
                            formatter:function (value,row,index) {
                                if(value){
                                    return '<span class="text-warning" >'+value+'</span>';
                                }
                            },
                        },
                        // {field: 'is_save', title: __('是否已测'),  searchList: {1: __('已测'), 0: __('未测')},
                        //     formatter:function (value,row,index) {
                        //         if(value==1){
                        //             return '<span class="text-success" >已测</span>';
                        //         }
                        //         if(value==0){
                        //             return '<span class="text-info" >未测</span>';
                        //         }
                        //     },
                        // },
                        {field: 'reliability', title: __('可信度'), operate: '=',searchList: {0: __('0'), 25: __('25%'), 50: __('50%'), 75: __('75%'), 100: __('100%')},
                            formatter:function (value,row,index) {
                                if(value>0){
                                    return value+'%';
                                }
                            },
                        },
                        {field: 'buttons', title: __('测试报告'),formatter:function (value,row,index) {
                                return '<a class="btn btn-success btn-file btn-xs btn-" href="/admin/projects/download?user_id='+row.id+'" title="下载测评报告">下载报告</a>';
                            },
                            operate:false
                        },
                        {field: 'operate', title: __('Operate'), table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.buttons,
                            buttons: [
                                {
                                    name: 'dispatch',
                                    text: '编辑',
                                    icon: 'fa fa-pencil',
                                    title: '编辑',
                                    classname: 'btn btn-success btn-xs btn-magic btn-dialog',
                                    // extend: 'data-area=\'["85%","85%"]\'',
                                    url: 'user/user/edit',
                                },
                                {
                                    name: 'dispatch',
                                    text: '清空',
                                    icon: 'fa fa-trash',
                                    title: '清空记录',
                                    visible: function (rr) {
                                        if(rr.test_text){
                                            return true;
                                        }
                                        return false;
                                    },
                                    classname: 'btn btn-danger btn-xs btn-magic btn-ajax',
                                    url: 'user/user/clear',
                                    confirm: '确定要清空测评记录吗？',
                                    success: function (data, ret) {
                                        $(".btn-refresh").trigger("click");
                                    },
                                    error: function (data, ret) {
                                        Layer.alert(ret.msg);
                                        return false;
                                    }
                                }
                            ],
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

            $(document).on("click", ".btn-batch", function () {
                var ids = Table.api.selectedids(table);
                if(ids.length==0){
                    layer.alert('请选择要下载的数据');
                    return false;
                }
                window.open('/admin/user/user/batch?ids='+ids);
            });
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});